package com.zjf.carpark.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.StorageClass;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.transfer.TransferManager;

/**
 * @author: zjf
 * @version:2020年5月14日 下午9:52:06
 * @Desc 腾讯云  cos对象存储
 */
public class TencentCosUtil_py {

    static String bucketName = "smartpark-1301017257"; //桶的名称
    static String region = "ap-nanjing";//区域南京
    static String appId = "1301017257"; //APPID
    static COSCredentials cred = null;
    static TransferManager transferManager = null;
    static ClientConfig clientConfig = null;

    static {
        // 1 初始化用户身份信息(secretId, secretKey)
        //SecretId 是用于标识 API 调用者的身份
        String SecretId = "AKIDWorfEXrXHm26ysPy71f3MDVYLsAJYpLm";
        //SecretKey是用于加密签名字符串和服务器端验证签名字符串的密钥
        String SecretKey = "LQHtFLCvYv2ulMhT0KPEn45qLZYIp6IQ";
        cred = new BasicCOSCredentials(SecretId, SecretKey);
        // 2 设置bucket的区域,
        clientConfig = new ClientConfig(new Region(region));
    }

    /**
     * @author zjf
     * @date 2020年5月14日下午10:05:08
     * @param key  key前面为路径，后面为文件名
     * @param filepath  上传文件的路径
     * @return String
     * @Des:  将本地文件上传到COS
     * "aaa/cc/cam.png","d://cam.png"
     */
    public static String SimpleUploadFileFromLocal(String key ,String filepath) {
        // 3 生成cos客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        File localFile = new File(filepath);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
        // 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
        putObjectRequest.setStorageClass(StorageClass.Standard);
        try {
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            // 关闭客户端
            cosClient.shutdown();
            return "https://"+bucketName+".cos."+region+".myqcloud.com/"+key;
        } catch (CosServiceException e) {
            e.printStackTrace();
        } catch (CosClientException e) {
            e.printStackTrace();
        }

        // 关闭客户端
        cosClient.shutdown();
        return null;
    }





    // 从输入流进行读取并上传到COS
    public static String SimpleUploadFileFromStream(String key,InputStream input) throws IOException {
        //  生成cos客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        // bucket名需包含appid
        ObjectMetadata objectMetadata = new ObjectMetadata();
        // 从输入流上传必须制定content length, 否则http客户端可能会缓存所有数据，存在内存OOM的情况
        objectMetadata.setContentLength(Integer.valueOf(input.available()).longValue());
        // 默认下载时根据cos路径key的后缀返回响应的contenttype, 上传时设置contenttype会覆盖默认值
        objectMetadata.setContentType("image/jpeg");

        PutObjectRequest putObjectRequest =
                new PutObjectRequest(bucketName, key, input, objectMetadata);
        // 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
        putObjectRequest.setStorageClass(StorageClass.Standard);
        try {
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            // 关闭客户端
            cosClient.shutdown();
            return "https://"+bucketName+".cos."+region+".myqcloud.com/"+key;
        } catch (CosServiceException e) {
            e.printStackTrace();
        } catch (CosClientException e) {
            e.printStackTrace();
        }

        // 关闭客户端
        cosClient.shutdown();
        return null;
    }



}